<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="耳标号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag v-decorator="['earLabel',validatorRules.earLabel]" dict="rpm_reg_matchregistry ,distinct ear_label,ear_label" :disabled="edit_disabled" />
            </a-form-item>
          </a-col>
<!--详细信息-->
          <a-col :span="12"  v-show="isShow">
            <a-form-item label="生长状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['growthStatus']" :trigger-change="true" dictCode="cattle_growth_status" disabled />
            </a-form-item>
          </a-col>

          <a-col :span="12"  v-show="isShow">
            <a-form-item label="繁殖状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['breedStatus']" :trigger-change="true" dictCode="cattle_breed_status" disabled/>
            </a-form-item>
          </a-col>

          <a-col :span="12"  v-show="isShow">
            <a-form-item label="当前胎次" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['currentParity']" :trigger-change="true"  disabled/>
            </a-form-item>
          </a-col>

          <a-col :span="12"  v-show="isShow">
            <a-form-item label="发情方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['estrusMode']" :trigger-change="true" dictCode="estrus_mode" disabled />
            </a-form-item>
          </a-col>

          <a-col :span="12"  v-show="isShow">
            <a-form-item label="出生日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"  placeholder="请选择出生日期" v-decorator="['birthDate']" :trigger-change="true" style="width: 100%"  disabled/>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="妊检日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date-limit  :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"  placeholder="请选择妊检日期" v-decorator="['prgancyExamDate',validatorRules.prgancyExamDate]" :trigger-change="true" style="width: 100%" :disabled="edit_disabled" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="妊检方法" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['prgancyExamMode',validatorRules.prgancyExamMode]" :trigger-change="true" dictCode="preg_exam_method" placeholder="请选择妊检方法" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="妊检结果" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['prgancyResults',validatorRules.prgancyResults]" :trigger-change="true" dictCode="preg_exam_conclusion" placeholder="请选择妊检结果" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="乳房充盈程度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag  type="list" v-decorator="['breastFilling',validatorRules.breastFilling]" :trigger-change="true" dictCode="breast_filling"  placeholder="请选择乳房充盈程度"  />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="复检" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-multi-select-tag type="checkbox" v-decorator="['isInspection',validatorRules.isInspection]" :trigger-change="true" dictCode="yn" placeholder="请选择是否复检" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="胎衣脱落时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"  placeholder="请选择胎衣脱落时间" v-decorator="['timePalcentaSheddintg']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="操作员" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag  v-decorator="['operatorId',validatorRules.operatorId]" dict="sys_user,realname,username" />
           </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
  import JSuperQuery from '@/components/jeecg/JSuperQuery.vue'
  export default {
    name: 'RpmRegPrgancyregistryForm',
    components: {
      JSearchSelectTag,
      JSuperQuery,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          earLabel: {
            rules: [
              { required: true, message: '请选择耳标号!'},
            ]
          },
          prgancyExamDate: {
            rules: [
              { required: true, message: '请选择妊检日期!'},
            ]
          },
          prgancyExamMode: {
            rules: [
              { required: true, message: '请选择妊检方式!'},
            ]
          },
          prgancyResults: {
            rules: [
              { required: true, message: '请选择妊检结果!'},
            ]
          },
          operatorId: {
            rules: [
              { required: true, message: '请选择操作员!'},
            ]
          },
          breastFilling: {
            rules: [
              { required: true, message: '请选择乳房充盈程度!'},
            ]
          },
          isInspection: {
            rules: [
              { required: true, message: '是否复检!'},
            ]
          },
        },
        url: {
          add: "/rpm/rpmRegPrgancyregistry/add",
          edit: "/rpm/rpmRegPrgancyregistry/edit",
          queryById: "/rpm/rpmRegPrgancyregistry/queryById"
        },
        isShow:false,
        edit_disabled:false
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
        this.edit_disabled=false;
      },
      edit (record) {
        this.isShow=false;
        this.edit_disabled=true;
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'earLabel','prgancyExamDate','prgancyExamMode','prgancyResults','isInspection','operatorId','delFlag','timePalcentaSheddintg','breastFilling'))
        })
      },
      moreinfo(record){//显示详细详细调用的方法
        this.isShow=true;
        this.edit_disabled=false;
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'earLabel','prgancyExamDate','prgancyExamMode','prgancyResults','isInspection','timePalcentaSheddintg','breastFilling','operatorId','birthDate','estrusMode','currentParity','breedStatus','growthStatus','birthDate'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'earLabel','prgancyExamDate','prgancyExamMode','prgancyResults','isInspection','timePalcentaSheddintg','breastFilling','operatorId'))
      },
    }
  }
</script>